﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace TeamV_BitmobileThickClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            OrderBiz biz = new OrderBiz();
            biz.getOrders();

        }

        private void orderBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            dataGridUpdate();
        }


        private void dataGridUpdate()
        {

            OrderBiz biz = new OrderBiz();
            Order.Order[] orders = biz.getOrders();

            List<Order.Order> list = new List<Order.Order>();
            list.Sort();

            comboBox2.Items.Clear();

            foreach (Order.Order toAdd in orders)
            {
                list.Add(toAdd);
                comboBox2.Items.Add(toAdd.id.ToString() );
            }

            dataGridView1.DataSource = list;

            dataGridView1.Refresh();

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox2.SelectedItem != null )
            {
                OrderBiz biz = new OrderBiz();

                int id = Int32.Parse(comboBox2.SelectedItem.ToString() );
                biz.setOrder( id, comboBox1.SelectedItem.ToString() );

                dataGridUpdate();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            updatePartsGrid();
        }


        private void updatePartsGrid() {

            WarehouseBiz biz = new WarehouseBiz();

            Warehouse.Parts[] parts = biz.getParts();

            List<Warehouse.Parts> list = new List<Warehouse.Parts>();
            list.Sort();
            comboBox3.Items.Clear();

            foreach (Warehouse.Parts toAdd in parts)
            {

                list.Add(toAdd);
                comboBox3.Items.Add(toAdd.name.ToString());
            }

            dataGridView2.DataSource = list;
            dataGridView2.Refresh();

        }

        private void button4_Click(object sender, EventArgs e)
        {

            WarehouseBiz biz = new WarehouseBiz();

            String stockName = "";
            int quantity = 0; 
            if (comboBox3.SelectedItem != null && comboBox4.SelectedItem != null)
            {
                 stockName = comboBox3.SelectedItem.ToString();
                 quantity  = Int32.Parse(comboBox4.SelectedItem.ToString());
            }

            biz.replenishStock(stockName, quantity);
            updatePartsGrid();

        }
    }
}
